package com.anrong.wulansdk;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.anrong.wulansdk.sdk.WLConfig;
import com.anrong.wulansdk.sdk.WLData;

import java.util.ArrayList;
import java.util.List;

public class NavigationActivity extends Activity {

	String processEventID = "playGame";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_navigation);
		
		initPermission();
		
		findViewById(R.id.btn_to).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				NavigationActivity.this.finish();
				startActivity(new Intent(NavigationActivity.this, MainActivity.class));
			}
		});
		
		
		
//		bindService(new Intent(this, TestService.class), new ServiceConnection() {
//			
//			@Override
//			public void onServiceDisconnected(ComponentName name) {
//				
//			}
//			
//			@Override
//			public void onServiceConnected(ComponentName name, IBinder service) {
//				MyBinder myBinder = (MyBinder)service;
//				TestService testService = myBinder.getService();
//				testService.show();
//			}
//		}, Context.BIND_AUTO_CREATE);
		
		
		
	}
	
	private void initWL(){
		//配置sdk
		WLConfig config = new WLConfig.Builder(getApplication())
//		.setSendMode(SendMode.SEND_MODE_TIME)
//		.setSendTime(10*1000)
//		.setShowLog(true)
		.setAppKey("66db3b9c5e835be0c5106ba09be912e8")
//		.setRun(true)
		.build();//这里可得到manifest 中的appkey 值
		//初始化
		WLData.init(config);
		
		WLData.setAccountKey("xxxyyy");
	}
	
	
	
	private final static int REQUEST_CODE = 0x123;	
	/**
	 * 获取权限
	 */
	private void initPermission() {
		
		if(Build.VERSION.SDK_INT >= 23){//判断当前系统的版本  
			
			//判断定位权限, 没有这个权限就无法获取定位信息
			String[] permissions = {
					Manifest.permission.WRITE_EXTERNAL_STORAGE,
					Manifest.permission.ACCESS_FINE_LOCATION
			};
			
			
			
			List<String> noPermissions = new ArrayList<String>();
			
			for (int i = 0; i < permissions.length; i++) {
				int hava = checkSelfPermission(permissions[i]);
				if (hava != PackageManager.PERMISSION_GRANTED) {
					noPermissions.add(permissions[i]);
				}
			}
			
			
			if (noPermissions.size() != 0) {
				requestPermissions(noPermissions.toArray(new String[noPermissions.size()]), REQUEST_CODE);
			}else {
				initWL();
			}
		}
		
	}



	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		if (requestCode == REQUEST_CODE) {
			
			if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
				//成功获取地理位置权限
			}
			
			if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
				//成功获取存储位置读写权限
				initWL();
			}else {
				Toast.makeText(this, "获取存储位置读写权限失败", 0).show();
			}
			
		}
	}
	
}
